package storage

import (
	"context"
	"github.com/brocaar/lorawan"
	"strings"

	log "application/v3/pkg/logger"
	"github.com/gofrs/uuid"
	"github.com/jmoiron/sqlx"
	"github.com/pkg/errors"

	"application/v3/internal/backend/networkserver"
	"github.com/brocaar/chirpstack-api/go/v3/ns"
)

func GetNSClient(n *NetworkServer) (ns.NetworkServerServiceClient, error) {
	nsClient, err := networkserver.GetPool().Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
	if err != nil {
		log.Warn("connect network-server error:", err)
		//这里本地的话要将chirpstack-network-server:8000 替换为localhost:8000，因为docker内部访问不到的
		serverName := strings.Replace(n.Server, "chirpstack-network-server", "localhost", 1)
		log.Info("connect network-server success,replace name is ", serverName)

		return networkserver.GetPool().Get(serverName, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
	}

	return nsClient, nil
}

func GetNetworkServerServiceByDeveui(ctx context.Context, db sqlx.Queryer, devEUI lorawan.EUI64) (ns.NetworkServerServiceClient, error) {
	n, err := GetNetworkServerForDevEUI(ctx, db, devEUI)
	if err != nil {
		return nil, err
	}

	nsClient, err := GetNSClient(&n)
	if err != nil {
		return nil, err
	}

	return nsClient, nil
}

func GetNetworkServerServiceById(ctx context.Context, db sqlx.Queryer, id int64) (ns.NetworkServerServiceClient, error) {
	n, err := GetNetworkServer(ctx, db, id)
	if err != nil {
		return nil, err
	}

	nsClient, err := GetNSClient(&n)
	if err != nil {
		return nil, err
	}

	return nsClient, nil
}

func GetNetworkServerServiceByGatewayMAC(ctx context.Context, db sqlx.Queryer, mac lorawan.EUI64) (ns.NetworkServerServiceClient, error) {
	n, err := GetNetworkServerForGatewayMAC(ctx, db, mac)
	if err != nil {
		return nil, err
	}

	nsClient, err := GetNSClient(&n)
	if err != nil {
		return nil, err
	}

	return nsClient, nil
}

func GetNetworkServerServiceByMulticastGroupID(ctx context.Context, db sqlx.Queryer, mgID uuid.UUID) (ns.NetworkServerServiceClient, error) {
	n, err := GetNetworkServerForMulticastGroupID(ctx, db, mgID)
	if err != nil {
		return nil, err
	}

	nsClient, err := GetNSClient(&n)
	if err != nil {
		return nil, err
	}

	return nsClient, nil
}

func GetNetworkServerByDeviceProfileID(ctx context.Context, db sqlx.Queryer, id uuid.UUID) (ns.NetworkServerServiceClient, error) {
	n, err := GetNetworkServerForDeviceProfileID(ctx, db, id)
	if err != nil {
		return nil, errors.Wrap(err, "get network-server error")
	}

	nsClient, err := GetNSClient(&n)
	if err != nil {
		return nil, errors.Wrap(err, "get network-server client error")
	}

	return nsClient, nil
}

func GetNetworkServerByServiceProfileID(ctx context.Context, db sqlx.Queryer, id uuid.UUID) (ns.NetworkServerServiceClient, error) {
	n, err := GetNetworkServerForServiceProfileID(ctx, db, id)
	if err != nil {
		return nil, errors.Wrap(err, "get network-server error")
	}

	nsClient, err := GetNSClient(&n)
	if err != nil {
		return nil, errors.Wrap(err, "get network-server client error")
	}

	return nsClient, nil
}
